//
// Created by 李光辉 on 2020/12/31.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>

const int MAX_STRING= 100;
int main()
{
    char  greeting[MAX_STRING];
    int comm_sz; //bumber of processes
    int my_rank; //myprocess rank
    int q;
    MPI_Init(NULL,NULL);
    MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
    MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);

    if(my_rank != 0)
    {
        sprintf(greeting,"Greetings from process %d of %d!",my_rank,comm_sz);
        MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,0,MPI_COMM_WORLD);
    }
    else
    {
        printf("Greetings from process %d of %d !\n",my_rank,comm_sz);
        for(q=1;q<comm_sz;q++)
        {
            MPI_Recv(greeting,MAX_STRING,MPI_CHAR,q,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
            printf("%s\n",greeting);
        }
    }

    MPI_Finalize();
    return 0;
}
/*
#编译
mpicc -g -Wall -o mpi_hello mpi_hello.c

#-g :产生供gdb调试用的可执行文件
#-Wall:编译后显示所有警告
#-o:输出到指定文件

运行方式：mpiexec -n 4 ./mpi_hello //使用4个进程执行程序
 */